package newsEventSystem;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Console extends WindowAdapter implements WindowListener,
		ActionListener, KeyListener {
	private static int number = 0;

	private JFrame frame;
	private JTextArea textArea;
	private int counter;
	int caretPosition;
	List<String> allLines;
	String lineBuffer;
	boolean quit;

	public Console(String title) {
		allLines = new ArrayList<String>();
		quit = false;
		lineBuffer = "";
		frame = new JFrame(title);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = new Dimension((int) (screenSize.width / 2),
				(int) (screenSize.height / 2));
		int x = (int) (frameSize.width / 2);
		int y = (int) (frameSize.height / 2);
		frame.setBounds(x, y, frameSize.width, frameSize.height);

		textArea = new JTextArea();
		textArea.setEditable(true);
		textArea.addKeyListener(this);

		JButton button = new JButton("clear");

		frame.getContentPane().setLayout(new BorderLayout());
		frame.getContentPane().add(new JScrollPane(textArea),
				BorderLayout.CENTER);
		frame.getContentPane().add(button, BorderLayout.SOUTH);
		frame.setVisible(true);

		frame.addWindowListener(this);
		button.addActionListener(this);
		number++;
		counter = number;

	}

	public void close() {
		frame.setVisible(false);
		frame.dispose();
		quit = true;
	}

	public void windowClosing(WindowEvent evt) {
		close();
	}

	public void actionPerformed(ActionEvent evt) {
		textArea.setText("");
	}

	public String getAuthorName() {
		String name = this.frame.getTitle() + getNumber();
		return name;
	}
	public int getNumber() {
		return counter;
	}

	public String readLine() {
		while (!quit && allLines.isEmpty()) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		if (quit) {
			return null;
		}
		String firstLine = allLines.get(0);
		allLines.remove(0);
		return firstLine;
	}

	public void writeLine(String text) {
		String input = textArea.getText() + text
				+ System.getProperty("line.separator");
		textArea.setText(input);
		textArea.setCaretPosition(input.length());
	}

	@Override
	 public void keyPressed(KeyEvent e) {
	  if (e.getKeyCode() == KeyEvent.VK_ENTER) {
	   allLines.add(lineBuffer);
	   lineBuffer = "";
	   return;
	  }
	  if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE){
		  lineBuffer=lineBuffer.substring(0,lineBuffer.length()-1);
		  return;
	  }
	  if (e.getKeyCode() != KeyEvent.VK_CAPS_LOCK
	     && e.getKeyCode() != KeyEvent.VK_SHIFT) {
	    lineBuffer += e.getKeyChar();
	   }
	 }

	@Override
	public void keyReleased(KeyEvent e) {
	}

	@Override
	public void keyTyped(KeyEvent e) {
	}

}
